/// @Classname : HcNoticeBar
/// @Author :
/// @Time : 2023-08-28 11:29
/// @Description : //todo

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_notice_bar_theme.dart';
import 'package:hc_ui/utils/hc_color_util.dart';
import 'package:hc_ui/widget/button/hc_outline_button.dart';
import 'package:hc_ui/widget/marquee/hc_marquee.dart';

import '../text_ellipsis/hc_text_ellipsis.dart';

typedef HcNoticeBarCallback = Function(String);

enum HcNoticeBarShowType {
  drag, //拖动
  scroll, //滚动
  ellipsis, //隐藏
  full, //展示全部
}

class HcNoticeBar extends StatelessWidget {
  //滚动方向
  final Axis scrollDirection;

  //左侧图标
  final Widget? leading;

  //尾部图标
  final Widget? trailing;

  //是否展示左侧按钮
  final bool showLeading;

  //是否展示右侧按钮
  final bool showTrailing;

  // 滚动的通知内容
  final String? content;

  //滚动通知的内容组 用于纵向滚动 如果横向滚动文字会拼接
  final List<String>? contentGroup;

  //不满一屏是否强制滚动
  final bool forceScroll;

  //中间的自定义组件
  final Widget? contentWidget;

  //组件的点击回调
  final HcNoticeBarCallback? callback;

  //展示的类型
  final HcNoticeBarShowType showType;

  final int? duration;

  final int maxLines;

  final double? minSize;
  final Color? textColor;
  final HcNoticeBarStyle? style;
  final HcNoticeBarTheme? themeData;

  const HcNoticeBar(
      {super.key,
      this.scrollDirection = Axis.horizontal,
      this.leading,
      this.trailing,
      this.callback,
      this.contentWidget,
      this.content,
      this.contentGroup,
      this.showType = HcNoticeBarShowType.scroll,
      this.forceScroll = false,
      this.duration,
      this.themeData,
      this.minSize,
      this.maxLines = 1,
      this.style,
      this.showLeading = true,
      this.showTrailing = true,
      this.textColor});

  @override
  Widget build(BuildContext context) {
    //获取快构建的颜色样式
    HcNoticeBarStyle finalStyle = style ?? HcNoticeBarStyle.normal;
    //获取组件的基础配置
    HcNoticeBarTheme theme = themeData ?? HcNoticeBarTheme();
    theme = HcCustomerConfig().allTheme.noticeBarTheme.merge(theme);
    //拼装最后的文字样式
    TextStyle textStyle = TextStyle(
        fontSize: theme.fontSize,
        fontWeight: theme.fontWeight,
        color: textColor ?? finalStyle.textColor);
    Widget? finalPrefixIcon =
        showLeading ? (leading ?? finalStyle.leadingIcon) : null;
    Widget? finalTrailingIcon =
        showTrailing ? trailing ?? finalStyle.trailingIcon : null;
    Widget buildContent() {
      if (contentWidget != null) {
        return contentWidget!;
      }
      //滑动或者拖动就是跑马灯效果
      if (showType == HcNoticeBarShowType.scroll ||
          showType == HcNoticeBarShowType.drag) {
        int time = (scrollDirection == Axis.horizontal
                ? (content ?? '').length
                : (contentGroup ?? []).join("").length)
            .toInt();
        return HcMarquee(
          duration: Duration(milliseconds: (duration ?? time * 1000)),
          textStyle: textStyle,
          forceScroll: forceScroll,
          enableScroll: showType == HcNoticeBarShowType.scroll,
          content: content,
          contentList: contentGroup,
          scrollDirection: scrollDirection,
        );
      }
      return HcTextEllipsis(
        separateShow: true,
        showEllipsisBtn: callback != null,
        content: content ?? contentGroup?.join("") ?? "",
        contentStyle: textStyle,
        expandText: "详情",
        maxLines: showType == HcNoticeBarShowType.full ? 2 * 31 : maxLines,
      );
    }

    Widget buildGestureDetector({name, child}) {
      return GestureDetector(
        child: child,
        onTap: () {
          if (callback != null) {
            callback!.call(name);
          }
        },
      );
    }

    return Container(
      padding: theme.padding,
      decoration: BoxDecoration(
          color: finalStyle.backgroundColor,
          borderRadius: BorderRadius.circular(theme.radius)),
      child: Row(
        children: [
          if (finalPrefixIcon != null)
            Container(
              margin: theme.leadingMargin,
              child: buildGestureDetector(
                  name: 'prefix',
                  child: IconTheme(
                      data: IconThemeData(
                          color: finalStyle.iconColor, size: theme.leadingSize),
                      child: finalPrefixIcon)),
            ),
          Expanded(
              child: buildGestureDetector(
            name: 'content',
            child: buildContent(),
          )),
          if (finalTrailingIcon != null)
            Container(
              margin: theme.trailingMargin,
              child: buildGestureDetector(
                  name: 'suffix',
                  child: IconTheme(
                      data: IconThemeData(
                          color: finalStyle.iconColor,
                          size: theme.trailingSize),
                      child: finalTrailingIcon)),
            ),
        ],
      ),
    );
  }
}

class HcNoticeBarStyle {
  //主要颜色
  Color mainColor;

  //为主颜色的叠加白色0.2
  Color backgroundColor;

  //文字颜色
  Color textColor;

  //图标颜色
  Color iconColor;

  //左图标组件
  Widget? leadingIcon;

  //右图表组件
  Widget? trailingIcon;

  HcNoticeBarStyle(
      {backgroundColor,
      required this.mainColor,
      String? btnText,
      textColor,
      iconColor,
      borderColor,
      this.leadingIcon,
      trailingIcon})
      : backgroundColor = backgroundColor ??
            HcColorUtil.mixColor(secondaryColor: mainColor, opacity: 0.2),
        textColor = textColor ??
                HcColorUtil.needLightColor(backgroundColor ??
                    HcColorUtil.mixColor(
                        secondaryColor: mainColor, opacity: 0.2))
            ? HcCustomerConfig().allTheme.commonTheme.textInverseColor
            : HcCustomerConfig().allTheme.commonTheme.textBaseColor,
        iconColor = iconColor ?? mainColor,
        trailingIcon = btnText != null
            ? HcOutlinedButton(
                text: btnText,
                onPressed: null,
                borderColor: borderColor ?? mainColor,
                textColor: textColor ?? mainColor,
                padding: const EdgeInsets.all(4),
                fontSize: 12,
                minSize: Size.zero,
              )
            : trailingIcon;

  // 默认的主题
  static HcNoticeBarStyle normal = HcNoticeBarStyle(
      mainColor: Colors.black,
      backgroundColor: Colors.transparent,
      iconColor: HcCustomerConfig().allTheme.commonTheme.primaryColor);

  //成功的主题
  static HcNoticeBarStyle success = HcNoticeBarStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.successColor,
      leadingIcon: const Icon(Icons.check_circle_outline_outlined),
      trailingIcon: const Icon(Icons.close_outlined));

  //错误的主题
  static HcNoticeBarStyle error = HcNoticeBarStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.errorColor,
      leadingIcon: const Icon(Icons.highlight_off_outlined),
      trailingIcon: const Icon(Icons.close_outlined));

  //警告的主题
  static HcNoticeBarStyle warning = HcNoticeBarStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.warningColor,
      leadingIcon: const Icon(Icons.error_outline_outlined),
      trailingIcon: const Icon(Icons.close_outlined));

  //帮助的主题
  static HcNoticeBarStyle help = HcNoticeBarStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.helpColor,
      leadingIcon: const Icon(Icons.help_outline_outlined),
      trailingIcon: const Icon(Icons.close_outlined));

  //通知的主题
  static HcNoticeBarStyle notice = HcNoticeBarStyle(
      mainColor: HcCustomerConfig().allTheme.commonTheme.noticeColor,
      leadingIcon: const Icon(Icons.info_outline_rounded),
      trailingIcon: const Icon(Icons.close_outlined));

  //成功的主题
  static HcNoticeBarStyle successWithButton({required String text}) =>
      HcNoticeBarStyle(
          mainColor: HcCustomerConfig().allTheme.commonTheme.successColor,
          leadingIcon: const Icon(Icons.check_circle_outline_outlined),
          btnText: text);

  //错误的主题
  static HcNoticeBarStyle errorWithButton({required String text}) =>
      HcNoticeBarStyle(
          mainColor: HcCustomerConfig().allTheme.commonTheme.errorColor,
          leadingIcon: const Icon(Icons.highlight_off_outlined),
          btnText: text);

  //警告的主题
  static HcNoticeBarStyle warningWithButton({required String text}) =>
      HcNoticeBarStyle(
          mainColor: HcCustomerConfig().allTheme.commonTheme.warningColor,
          leadingIcon: const Icon(Icons.error_outline_outlined),
          btnText: text);

  //帮助的主题
  static HcNoticeBarStyle helpWithButton({required String text}) =>
      HcNoticeBarStyle(
          mainColor: HcCustomerConfig().allTheme.commonTheme.helpColor,
          leadingIcon: const Icon(Icons.help_outline_outlined),
          btnText: text);

  //通知带按钮的主题
  static HcNoticeBarStyle noticeWithButton({required String text}) =>
      HcNoticeBarStyle(
          mainColor: HcCustomerConfig().allTheme.commonTheme.noticeColor,
          leadingIcon: const Icon(Icons.info_outline_rounded),
          btnText: text);
}
